import { defineStore } from "pinia";
import { post } from "@/utils/index"
import cookie from "js-cookie"
import { ElMessage } from "element-plus";
import router from "@/router";
export const useUserStore = defineStore('userStore',{// 状态管理的ID
    persist:true,       //持久化
    state: () => ({
        user : new UserInfo(),//用户信息
        userInfo:{
            username:'',
            password:''
        }
    }),
    actions : {
        // 同步
        async loginOut(){
            post({
                _gp:'admin',
                _mt:'logout',
            }).then((res)=>{
                if (200 === res.errno) {
                    this.user = new UserInfo()
                    cookie.remove("token")
                    router.replace("/login")
                    ElMessage.success("已退出登录")
                }
            })
        }
    }
})
// class类定义数据类型
class UserInfo {
    avatarUrl?: string;
    gmtCreate: number;
    gmtLastLogin: string;
    gmtUpdate: number;
    id: number;
    lastLoginIp: string;
    perms: string[];
    phone: string;
    realname: string;
    roleIds: number[];
    roles: string[];
    status: number;
    username: string;
    // 构造器
    constructor(){
        this.avatarUrl =  '';
        this.gmtCreate =  0;
        this.gmtLastLogin =  '';
        this.gmtUpdate =  0;
        this.id =  0;
        this.lastLoginIp =  '';
        this.perms =  [''];
        this.phone =  '';
        this.realname =  '';
        this.roleIds =  [];
        this.roles =  [''];
        this.status =  0;
        this.username =  '';
    }
}
